package com.cn.jx.manager.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.manager.po.Pwd;

/**
 * <p>PwdRepository 查询密码mapper层操作数据库的接口
 * <p>实现<{@link BaseIdRepository <tt>BaseIdRepository</tt>}
 * 
 * @author <h1>jiangxu2  2019年11月21日</h1>
 * @version v1.11.20.2019
 * @since 1.0
 * 
 */
public interface PwdRepository extends JpaRepository<Pwd,String>,JpaSpecificationExecutor<Pwd>{
	
	/**<p>定义HQL查询 查询当前有效密码根据userid {@link Pwd} <p>有效信息latest=1*/
	@Query("from Pwd  where userId=:pid and latest=1 ")
	public Pwd findLatestPwdByUserId(@Param(value = "pid") String pid);
	
	/**<p>定义HQL查询 查询历史有效密码根据userid {@link Pwd} <p>latest=0*/
	@Query("from Pwd  where userId=:pid and latest=0 ")
	public List<Pwd> findHistoryPwdByUserId(@Param(value = "pid") String pid);
	
	/**<p>定义HQL查询 更密码 更新为历史密码 {@link Pwd} */
	@Transactional
	@Modifying(clearAutomatically = true)
	@Query(value = "update Pwd p set p.latest =0 where p.userId =:userId")
	 int updatePwdByUserId(@Param(value = "userId") String userId);
}
